package com.pignest.hotel.ui.selfhelp;

import java.util.Date;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.DateUtils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.LivingCustom;
import com.pignest.hotel.beans.LivingSpecifics;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.LivingType;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class SelfLivingWindow extends Window implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 27151190133596489L;
	public RoomService roomService;
	public MemberService memberService;
	public ReceptionService receptionService;
	public SelfLivingWindow(){
		roomService = Services.getRoomService();
		memberService =Services.getMemberService();
		receptionService=Services.getReceptionService();
		addEventListener("onCreate", this);
	}
	
	private Label welcomceMesage;
	private Label message;
	private Label memberLevel;
	private Label memberType;
	private Label memberBlance;
	private Label memberIntegal;
	
	private Label roomName;
	private Label roomType;
	private Label roomPrice;
	private Label memberPrice;
	
	
	private Datebox leaveDate;
	private Listbox leaveTime;
	
	private Button selfLivingBtn;
	
	public Member curMember;
	public String roomId;
	private Room selectRoom;
	private String card;
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			welcomceMesage =(Label)getFellow("welcomceMesage");
			message =(Label)getFellow("message");
			memberLevel=(Label)getFellow("memberLevel");
			memberType=(Label)getFellow("memberType");
			memberBlance=(Label)getFellow("memberBlance");
			memberIntegal=(Label)getFellow("memberIntegal");
			
			roomName=(Label)getFellow("roomName");
			roomType=(Label)getFellow("roomType");
			roomPrice=(Label)getFellow("roomPrice");
			memberPrice=(Label)getFellow("memberPrice");
			
			leaveDate=(Datebox)getFellow("leaveDate");
			leaveTime=(Listbox)getFellow("leaveTime");
			
			selfLivingBtn=(Button)getFellow("selfLivingBtn");
			
			//初始化数据
			setInfo();
		}
		
	}
	public void setInfo()
	{
		if(this.curMember==null)
		{
			UIUtils.alert("无效的会员！");
			this.detach();
		}else{
			if(this.curMember.getMemberLevel()!=null)
			   memberLevel.setValue(this.curMember.getMemberLevel().getMemberlevel());
			else
			   memberLevel.setValue("未设置级别！");
			if(this.curMember.getType()!=null)
				memberType.setValue(this.curMember.getType());
			else
				memberType.setValue("未设置类型");
			Integer b=this.curMember.getBalance();
			memberBlance.setValue(MoneyUtil.toString(b));
			Integer I =this.curMember.getIntegral();
			memberIntegal.setValue(String.valueOf(I));
			
			String sSex ="先生";
			if(this.curMember.getSex()=="女")
				sSex ="小姐";
			welcomceMesage.setValue(this.curMember.getName()+sSex);
			leaveDate.setValue(new Date(System.currentTimeMillis() + DateUtils.ONE_DAY));
		}
		if(this.roomId==null)
		{
			UIUtils.alert("请选择房间！");
			this.detach();
		}else
		{
			Room room=roomService.getRoom(this.roomId);
			this.setSelectRoom(room);
			roomName.setValue(room.getName());
			roomType.setValue(room.getType().getName());
			roomPrice.setValue(MoneyUtil.toString(room.getType().getPrice()));
			int mP=memberService.getMemberPrice(this.curMember.getId(), room.getType().getId());
			memberPrice.setValue(MoneyUtil.toString(mP));
		}
		
	}
	//待完善，读卡写卡未完成
	public String selfLiving()
	{
		Date d1 = leaveDate.getValue();
		String time = UIUtils.getSelectedValue(leaveTime);
		d1 = DateUtil.parseDate(d1, time);
		if (d1.getTime() < System.currentTimeMillis() + DateUtil.ONE_HOUR) {
				return "预离时间填写不正确";
			}
		int mP=memberService.getMemberPrice(this.curMember.getId(), selectRoom.getType().getId());
		int balance=this.curMember.getBalance();
		if(this.curMember.getMemberLevel().getSelfhelplive().equals("否"))
		{
			//UIUtils.alert("很抱歉当前会员不能自助入住！请联系前台");
			return "很抱歉当前会员不能自助入住！请联系前台";
		}
		if(balance<Hotel.self_living_preMoney){
			//UIUtils.alert("当前余额不足，请充值！");
			return "当前余额不足，请充值！";
		}
		StringBuffer msg=new StringBuffer();
		Living l = new Living();
		l.setLiveType(LivingType.SELF_LIVING.name());
		l.setRoom(this.selectRoom);
		l.setMember(this.curMember);
		l.setLiveTime(new Date());
		l.setLiveState(LivingState.LIVING.name());
		l.setCustormName(this.curMember.getName());
		l.setSex(this.curMember.getSex());
		l.setPhone(this.curMember.getMobile());
		l.setPassportType(this.curMember.getCertificatType());
		l.setPassportNumber(this.curMember.getCertificateNo());
		l.setRoomPrice(this.selectRoom.getType().getPrice());
		
		l.setAdjustPrice(mP);
		l.setSelfHelp(true);
		l.setStorage(Hotel.self_living_preMoney);
		this.curMember.setBalance(balance-Hotel.self_living_preMoney);
		l.setAvailable(l.getStorage());
		l.setUsed(0);
		l.setCardNumber(this.card);
		Date d = DateUtil.parseDate(leaveDate.getValue(), (String)UIUtils.getSelectedValue(leaveTime));
		l.setGrantDate(d);
		l.setExpectLeaveDate(d);
		
		if (l.getSerialNumber() == null)
			l.setSerialNumber(Global.getLivineSerialNumber());
		LivingSpecifics spec = l.renderSpecifics();
		spec.setPaymentMethod("会员卡");
		
		/**初始设置账*/

			TbAccounts accounts=new TbAccounts();
			accounts.setLiving(l);
			//accounts.setType("押金");
			accounts.setType(InComeItemType.YJ.name());
			accounts.setPrice(Hotel.self_living_preMoney);
			accounts.setCounts(1);
			accounts.setEndstate("否");
			accounts.setName(InComeItemType.YJ.getValue());
			accounts.setOptime(new Date());
			accounts.setPrice2(Hotel.self_living_preMoney);
		/***设置主要客人**/
		LivingCustom lc = new LivingCustom();
		lc.setLiving(l);
		lc.setName(this.curMember.getName());
		lc.setIshost("是");
		lc.setCertificatetype(this.curMember.getCertificatType());
		lc.setCertificateno(this.curMember.getCertificateNo());
		lc.setPhone(this.curMember.getMobile());
		lc.setSex(this.curMember.getSex());

		String respectfully="";//尊称
		if(curMember.getSex().equals("男"))
			respectfully="先生";
		else
			respectfully="小姐/女士";
		msg.append("欢迎你:").append(this.curMember.getName()+respectfully).append("\n");
		msg.append("当前房间为:").append(this.selectRoom.getName()).append("\n");
		msg.append("会员价为:").append(MoneyUtil.toString(mP)).append("\n");
		if(UIUtils.confirm(msg.toString())){
			this.selectRoom.setLiveStatus(RoomLiveStatus.FILLED.name());
			Services.getRoomService().updateRoom(this.selectRoom);

			//receptionService.updateSelfLiving(l, spec, lc, selectRoom, accounts);
			return null;
		}
		return "";
	}
	public void writeCard()
	{
		Clients.evalJavaScript("printAlert()");
	}
	public Member getCurMember() {
		return curMember;
	}
	public void setCurMember(Member curMember) {
		this.curMember = curMember;
	}
	public String getRoomId() {
		return roomId;
	}
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}
	public Room getSelectRoom() {
		return selectRoom;
	}
	public void setSelectRoom(Room selectRoom) {
		this.selectRoom = selectRoom;
	}
	public String getCard() {
		return card;
	}
	public void setCard(String card) {
		this.card = card;
	}

}
